<?php
/**
 * +---------------------------------------------------------------
 * | Author: 北京永青宇创科技有限公司
 * | Date: 2020/03/12
 * | 版本: 6.0
 * +---------------------------------------------------------------
 * | CopyRight: 北京永青宇创科技有限公司 All rights reserved.
 * +---------------------------------------------------------------
 * | 版权声明：云豹不是一个自由软件，是云豹官方推出的商业源码。
 * | 严禁在未经许可的情况下拷贝、复制、传播、使用云豹的任意代码
 * | 如有违反，请立即删除，否则您将面临承担相应，法律责任的风险。
 * | 如果需要取得官方授权，请联系官方 https://www.gepardshop.com
 * +---------------------------------------------------------------
 */

namespace app\ticket\controller\admin;;
use app\common\builder\FormBuilder;
use app\common\builder\SortBuilder;
use app\common\builder\ListBuilder;
class TicketSolution extends TicketAdmin {
	protected $Model;

	public function initialize()
	{
		parent::initialize();
		$this->Model = model('TicketSolution');
	}

	public function Index($page = 1, $r = 20)
	{
		$map = [];
		//指定产品和问题类型下的解决方案
        $product_id = input('product_id');
        $category_id = input('category_id');
        if( ! empty($category_id) && ! empty($product_id)){
            $map['product_id'] = $product_id;
            $map['category_id'] = $category_id;
        }

		//关键字搜索
		$keyword = input('keyword');
		if( ! empty($keyword)) {
			$map['title'] = ['like', '%' . $keyword . '%'];
		}

		$status = input('status');
		if( ! empty($status)) {
			$map['status'] = $status;
		}
		//时间搜索
		$start_time = input('create_time');
		if( ! empty($start_time)) {
			$start_time_arr = explode('~', $start_time);
			list($start_one, $start_end) = \yunbao\time::getSomeday($start_time_arr[0]);
			list($end_one, $end_end) = \yunbao\time::getSomeday($start_time_arr[1]);
			$map['create_time'] = ['between', [$start_one, $end_end]];
		}

		list($list, $pagehtml) = $this->Model->where($map)->pagedata($r);



		//查询关联数据，此逻辑需要优化
		if( ! empty($list)) {
			foreach($list as &$item) {
				// $item['name'] = get_info_title('module/table', $item['id'], 'title', '不存在');
			}
		}
		$builder = new ListBuilder();

		//处理标题和按钮
		$builder->setMetaTitle('解决方案管理')
		        ->addTopButton('addnew', ['href' => url_admin('edit', $map)])
			//->addTopButton('self', ['title' => '排序', 'href' => url_admin('nest', $map)])
			    ->addSearchItem('status', 'select', '状态', '状态', ['1' => '正常', '0' => '已禁用', '-1' => '已删除'])
		        ->addSearchItem('create_time', 'dateranger', '发布时间', '发布时间')
		        ->addSearchItem('keyword', 'text', '关键字', '关键字')
		        ->setTableDataList($list)
		        ->setTableDataPage($pagehtml)
			//->addRightButton('self', ['title' => '管理', 'href' => url_admin('module/controller/action', ['id' => '__data_id__'])])
			    ->addRightButton('info', ['name' => 'info', 'title' => '查看', 'href' => url_admin('info', ['id' => '__data_id__'])])
		        ->addRightButton('edit', ['href' => url_admin('edit', ['id' => '__data_id__'])])
		        ->addRightButton('finaldelete', ['model' => 'ticket/TicketSolution']);

		//处理字段
		$builder->addTableColumn('id', '编号', 'id');
		$builder->addTableColumn('title', '标题', 'text');
//		$builder->addTableColumn('user_id', '用户id', 'uid');
		$builder->addTableColumn('product_id', '产品id', 'foreign_id');
		$builder->addTableColumn('category_id', '分类id', 'foreign_id');
		//$builder->addTableColumn('content', '详细内容', 'editor');
		$builder->addTableColumn('num_support', '支持数', 'integer');
		$builder->addTableColumn('num_oppose', '反对数', 'integer');
		$builder->addTableColumn('status', '状态', 'toggle');
		$builder->addTableColumn('sort', '排序', 'integer');
		$builder->addTableColumn('create_time', '创建时间', 'datetime');
		$builder->addTableColumn('update_time', '更新时间', 'datetime');

		$builder->addTableColumn('right_button', '操作', 'btn');

		return $builder->display();
	}

	public function info($id = 0)
	{
		$map     = [];
		$builder = new FormBuilder();

		if($id != 0) {
			$info = $this->Model->find($id);
		} else {
			$info = $map;
		}

		//处理标题
		$builder->setMetaTitle('解决方案')
		        ->setPostUrl(url_admin('edit'))
		        ->setTemplate('layout/ucenter/info')
		        ->setBuilder('info')
		        ->setFormData($info);

		//处理字段
		$builder->addFormItem('id', 'id', '编号');
		$builder->addFormItem('title', 'text', '标题');
		$builder->addFormItem('user_id', 'uid', '用户id');
		$builder->addFormItem('product_id', 'foreign_id', '产品id');
		$builder->addFormItem('category_id', 'foreign_id', '分类id');
		$builder->addFormItem('content', 'editor', '详细内容');
		$builder->addFormItem('num_support', 'integer', '支持数');
		$builder->addFormItem('num_oppose', 'integer', '反对数');
		$builder->addFormItem('status', 'toggle', '状态');
		$builder->addFormItem('sort', 'integer', '排序');
		$builder->addFormItem('create_time', 'datetime', '创建时间');
		$builder->addFormItem('update_time', 'datetime', '更新时间');

		return $builder->display();
	}

	public function edit($id = 0)
	{
		$map = [];
		if(request()->isPost()) {
			$data = input('');
			if($id != 0) {
				if($this->Model->allowField(true)->save($data, ['id' => $id])) {
					$this->success('操作成功', url_admin('index', $map));
				} else {
					$this->error('操作失败');
				}
			} else {

				if($this->Model->allowField(true)->save($data)) {
					$this->success('操作成功', url_admin('index', $map));
				} else {
					$this->error('操作失败');
				}
			}
		} else {
			$builder = new FormBuilder();

			if($id != 0) {
				$info = $this->Model->find($id);
			} else {
				$info = $map;
			}

			//处理标题
			$title_info = $id > 0 ? '编辑' : '新增';
			$builder->setMetaTitle('解决方案' . $title_info)
			        ->setPostUrl(url_admin('edit'))
			        ->setFormData($info);

			//处理字段
			$builder->addFormItem('id', 'id', '编号');
			$builder->addFormItem('title', 'text', '标题');
			//$builder->addFormItem('user_id', 'uid', '用户id');
			$builder->addFormItem('product_id', 'foreign_id', '产品id','',\yunbao\modelhelp::get_foreign_list('ticket/ticketProduct'));
			$builder->addFormItem('category_id', 'foreign_id', '分类id','',\yunbao\modelhelp::get_foreign_list('ticket/ticketCategory'));
			$builder->addFormItem('content', 'editor', '详细内容');
			$builder->addFormItem('num_support', 'integer', '支持数');
			$builder->addFormItem('num_oppose', 'integer', '反对数');
			$builder->addFormItem('status', 'toggle', '状态');
			$builder->addFormItem('sort', 'integer', '排序');
			$builder->addFormItem('create_time', 'datetime', '创建时间');
			$builder->addFormItem('update_time', 'datetime', '更新时间');

			return $builder->display();
		};
	}

	public function del($id = 0)
	{
		$id = array_unique((array)input('id', 0));

		if(empty($id)) {
			$this->error(lang('_ERROR_DATA_SELECT_') . lang('_EXCLAMATION_'));
		}

		$map = ['id' => ['in', $id]];
		if($this->Model->where($map)->delete()) {

			$this->success('操作成功');
		} else {
			$this->error('操作失败');
		}
	}
}